<?php

declare(strict_types=1);

namespace App\CustHub\Service;

use App\CustHub\Mapper\ChContactMapper;
use App\CustHub\Mapper\ChSmartTagChangeLogMapper;
use App\CustHub\Model\ChContact;
use App\CustHub\Model\ChSmartTag;
use Carbon\Carbon;
use Hyperf\Di\Annotation\Inject;
use Mine\Abstracts\AbstractService;

class ChSmartTagChangeLogService extends AbstractService
{
    public $mapper;

    #[Inject]
    protected ChContactMapper $contactMapper;

    public function __construct(ChSmartTagChangeLogMapper $mapper)
    {
        $this->mapper = $mapper;
    }

    /**
     * @param mixed $tagId
     * @param array $contactIdArr
     * @param int $type
     * @return void
     */
    public function saveLog(mixed $tagId, array $contactIdArr, int $type = 0): void
    {
        $tagGroupId = ChSmartTag::query()->where('id', $tagId)->value('parent_id');
        $batchData = [];
        $contactList = ChContact::query()
            ->whereIn('id', $contactIdArr)
            ->pluck('robot_id','id')
            ->toArray();

        foreach ($contactList as $contactId => $robotId ){
            $batchData[] = [
                'tag_group_id' => $tagGroupId,
                'tag_id' => $tagId,
                'contact_id' => $contactId,
                'robot_id' => $robotId,
                'type' => $type,
                'created_at' => Carbon::now()->toDateTimeString()
            ];
        }
        $this->mapper->model::insert($batchData);
    }

    /**
     * 变更记录
     * @param array $data
     * @return array
     */
    public function getLogList(array $data): array
    {
        return $this->mapper->getLogList($data);
    }
}